package gui.catalogues.colors.models;

import gui.catalogues.colors.ColorFormEvent;
import gui.catalogues.colors.interfaces.ColorFormListener;
import gui.commons.CommonLayoutOperations;
import gui.commons.ToolbarListener;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class ColorFormPanel extends JPanel {
	
	/*
	 *	Variables declaration
	 *
	 * */
	private ToolbarListener sl;
	private Border innerBorder = BorderFactory.createTitledBorder("ABC Colores"), 
		outerBorder = BorderFactory.createEmptyBorder(15, 15, 15, 15); 
	private Border compoundBorder = BorderFactory.createCompoundBorder(
		outerBorder, innerBorder);
	Dimension dim = getPreferredSize();
	//private JLabel name, surname;
	private JTextField tfColor, tfCCode;
	private JButton ok;
	private ColorFormListener fl;
	
	/*
	 * layout variables declaration
	 * */
	private int y;
	private GridBagConstraints gc;
	private Insets lblInset, tfInset, btnInset;
	
	/*
	 * Repetitive tasks class variable
	 */
	private CommonLayoutOperations cOps;
	
	public ColorFormPanel() {
		
		/*
		 * Initializers and population
		 */
		initAndPopulate();

		/*
		 * Generate the panel's layout
		 */
		generatePanelLayout();

		/*
		 * ActionListeners
		 */
		generateActionListeners();
	}
	
	public void initAndPopulate() {
		/*
		 * Initializing variables
		 * 
		 * */
		//name = new JLabel("Nombre: ");
		//surname = new JLabel("Apellidos: ");
		tfColor = new JTextField(2);
		tfCCode = new JTextField(2);
		ok = new JButton("Aceptar");
		
		
		/*
		 * layout variables
		 * */
		gc = new GridBagConstraints();
		lblInset = new Insets(0, 0, 0, 2);
		tfInset = new Insets(0, 2, 0, 2);
		btnInset = new Insets(2, 2, 2, 2);
		
		/*
		 * Common tasks variable
		 * */
		cOps = new CommonLayoutOperations(lblInset, tfInset, btnInset);

	}
	
	public void generatePanelLayout() {
		/*
		 *	setting size and borders for the Panel
		 *
		 */
		dim.height = 350;
		setPreferredSize(dim);
		
		setBorder(compoundBorder);
		
		/*
		 *	Manage Layout
		 * 
		 * */
		setLayout(new GridBagLayout());
		gc = new GridBagConstraints();
		//	position x, y
		gc.gridx = 0;
		gc.gridy = 0;
		//	padding (minimum space left between component and enclosing cell
		gc.ipadx = 5;
		gc.ipady = 3;
		//	space relative to other cells around
		gc.weightx = 0.5;
		gc.weighty = 0.1;
		//	Take or not the space left in the layout
		gc.fill = GridBagConstraints.NONE;
		
		/*
		 *	Popullate the grid with its components
		 * 
		 * */
		y = 0;
		cOps.addJLabel(this, "Color: ", 0, y);
		cOps.addTextField(this, tfColor, 1, y);
		y++;	// 1
		cOps.addJLabel(this, "Color code: ", 0, y);
		cOps.addTextField(this, tfCCode, 1, y);
		y++;	// 2
		cOps.addJButton(this, ok, 2, y);
	}
	
	public void generateActionListeners() {
		/*
		 *	ActionListeners
		 * 
		 * */
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ColorFormEvent fe = new ColorFormEvent(e, tfColor.getText(), tfCCode.getText());
				if(fl != null) {
					fl.ColorFormEventOccured(fe);
				}
			}
		});
	}
	
	//	method to assign a textPanel to the formPanel so the latter will be listened
	public void setStringListener(ToolbarListener sl) {
		this.sl = sl;
	}
	
	/*
	 * FormListener
	 * 
	 * */
	public void setColorFormListener(ColorFormListener fl) {
		this.fl = fl;
	}
	
//	public void addLabel(String lbl, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_END;
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 5;
//		gc.ipady = 1;
//		gc.insets = lblInset;
//		add(new JLabel(lbl), gc);
//	}
//	
//	public void addTextField(JTextField t, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		t.setMinimumSize(t.getPreferredSize());
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 110;
//		gc.insets = tfInset;
//		add(t, gc);
//	}
//	
//	public void addButton(JButton b, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 2;
//		gc.ipady = 1;
//		gc.insets = btnInset;
//		add(b, gc);
//	}
}
